#!/bin/sh

if [ "${branch}" = "master" ]; then
    branch="rawhide"
fi

base_dir="$(dirname $0)/.."
HOMEDIR=$(pwd)

# Make sure logs are empty so we don't report previous build
# if we fail
if [ -e "${HOMEDIR}/output/logs" ]; then
    sudo rm -rf ${HOMEDIR}/output/logs >/dev/null 2>&1
    echo "rm -rf ${HOMEDIR}/output/logs rc:$?"
fi
mkdir -p ${HOMEDIR}/output

pushd $base_dir/config/Dockerfiles/rsync
sudo docker build -t rsync-container .
popd

pushd $base_dir/config/Dockerfiles/ostree_compose
sudo docker build -t ostree_compose-container .
popd

sudo docker run --privileged --cap-add=SYS_ADMIN -v ${HOMEDIR}/output:/home/output -e rsync_paths="ostree" -e rsync_from="${RSYNC_USER}@${RSYNC_SERVER}::${RSYNC_DIR}/${branch}/" -e rsync_to="/home/output/" -e RSYNC_PASSWORD="$RSYNC_PASSWORD" rsync-container
echo $?

sudo docker run --privileged --cap-add=SYS_ADMIN -v ${HOMEDIR}/output:/home/output -e branch="${branch}" -e HTTP_BASE="${HTTP_BASE}" ostree_compose-container
echo $?

# Logs need to be in a specific place to be picked up.
sudo mv ${HOMEDIR}/output/logs ${HOMEDIR}
echo $?

sudo docker run --privileged --cap-add=SYS_ADMIN -v ${HOMEDIR}/output:/home/output -e rsync_paths="ostree" -e rsync_to="${RSYNC_USER}@${RSYNC_SERVER}::${RSYNC_DIR}/${branch}/" -e rsync_from="/home/output/" -e RSYNC_PASSWORD="$RSYNC_PASSWORD" rsync-container
echo $?
